<?php

namespace app\doc\controller;

use think\Controller;
use utils\Document\ClassReader;
use utils\Document\Document;

class Index extends Controller {

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * @title 测试-加载index控制器下面的方法
     */
    public function index2() {



        $api_base_dir = APP_PATH . 'index' . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;

        // 获取api控制器下面所有的接口文件夹
        $dirs = my_scan_dir($api_base_dir . '*', GLOB_ONLYDIR);
        $this->assign('dirs', $dirs);

        $dir = input('param.dir', '');
        $this->assign('dir', $dir);



        $dir_all = $api_base_dir;

        $service_annotation = $this->_get_service_annotation($dir_all);


        $this->assign('service_annotation', $service_annotation);

        //加载统计信息
        $statistics_lists_2 = [];
//        $statistics_lists = model('statistics')->select();
//        foreach ($statistics_lists as $key => $value) {
//            $statistics_lists_2[$value['api_url']] = $value;
//        }





        return view();
    }

    /**
     * @title 接口列表
     */
    function index() {



        $api_base_dir = APP_PATH . 'api' . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;

        // 获取api控制器下面所有的接口文件夹
        $dirs = my_scan_dir($api_base_dir . '*', GLOB_ONLYDIR);
        $this->assign('dirs', $dirs);


        if (empty($dirs)) {
            $this->error('没有任何接口被发现');
        }


        $dir = input('param.dir', $dirs[0]);
        $this->assign('dir', $dir);



        $dir_all = $api_base_dir . $dir;

        $service_annotation = $this->_get_service_annotation($dir_all);


        $this->assign('service_annotation', $service_annotation);

        //加载统计信息
        $statistics_lists_2 = [];
//        $statistics_lists = model('statistics')->select();
//        foreach ($statistics_lists as $key => $value) {
//            $statistics_lists_2[$value['api_url']] = $value;
//        }



        $this->assign('statistics_lists', $statistics_lists_2);


        return view();
    }

    /**
     * @title 单接口测试
     */
    function views() {

        $dir = input('get.dir', '');
        $controller = input('get.controller', '');
        $method = input('get.method', '');



        $api_base_dir = APP_PATH . 'api' . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;

        // 获取api控制器下面所有的接口文件夹
        $dirs = my_scan_dir($api_base_dir . '*', GLOB_ONLYDIR);
        $this->assign('dirs', $dirs);

        empty($dirs) && $this->error('没有任何接口被发现');


       // $dir = input('param.dir', $dirs[0]);
       // $this->assign('dir', $dir);




        $dir_all = $api_base_dir . $dir;


        $service_annotation = $this->_get_service_annotation($dir_all);
        
        
       // print_r($service_annotation);exit;
        



        $configList = $service_annotation[$controller]['child'][$method];


        //print_r($configList);exit;

        if (!$configList) {
            echo "Error : can not found '$controller::$method'.\n";
            exit;
        }

        // $result['action_val'] = url('api' . '/' . $dir . '.' . strtolower($controller) . '/' . $method);
        $result['action_val'] = 'api.php' . '/' . $dir . '.' . strtolower($controller) . '/' . $method;



        //print_r( $result['action_val']);exit;



        $result['method_val'] = $configList['method'];

        $result['title'] = $configList['title'];

        $key_val_html = '';

        //当有参数的时候
        if (isset($configList['params'])) {

            foreach ((array) $configList['params'] as $paramName => $paramData) {
                $paramDval = $paramData['dval']; // default value
                $paramDesc = $paramData['desc']; // description
                $key_val_html .= " <div style='margin-bottom:5px;'> Key : <input type='text' name='paramKey' value='{$paramName}'/> Value : <input type='text' name='paramVal' style='width:300px' value='$paramDval'/> {$paramDesc} </div>\n";
            }
        }

        $result['key_val_html'] = $key_val_html;

        $this->assign('result', $result);
        $this->assign('dir', $dir);

        // dump($result);

        return view('views');
    }

    /**
     * @title 获取各个类的各个方法的注释字段，返回二维+数组
     */
    private function _get_service_annotation($dir) {


        $module_list = array();

        //类名&方法名解析类
        $class_reader = new ClassReader($dir);
        $service_tree = $class_reader->get_service_tree();
        // print_r($service_tree);exit;
        foreach ($service_tree as $classes => $methods) {
            $class_file = $dir . '/' . $classes . '.php';
            $class_name = $classes;
            //注释解析类
            $my_doc = new Document($class_file);
            $class_annotation = $my_doc->getAnnotation($class_name);
            $module_list[$classes] = $class_annotation;
            //依次循环输出方法名 
            foreach ($methods as $k2 => $method_name) {
                $method_annotation = $my_doc->getAnnotation($class_name, $method_name);
                if ($method_annotation) {
                    $module_list[$classes]['child'][$method_name] = $method_annotation;
                }
            }
        }
        return $module_list;
    }

}
